package com.laoli.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.entity.CourseComment;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户表(User)表数据库访问层
 *
 * @author makejava
 * @since 2020-09-07 17:26:30
 */
@Repository
public interface CourseCommentDao extends BaseMapper<CourseComment> {

    /**
     * 某个课程的全部留言（分页）
     * @param courseid 课程编号
     * @param offset 数据偏移
     * @param pageSize 每页条目数
     * @return 留言集合
     */
    @Select({"select id,`course_id`,`section_id`,`lesson_id`,user_id,`user_name`,`parent_id`,`is_top`,`comment`,`like_count`,`is_reply`,`type`,`status`,create_time ,update_time ,is_del,`last_operator`,`is_notify`,`mark_belong`,`replied`\n" +
            "            from course_comment\n" +
            "            where is_del = 0\n" +
            "            and course_id =#{courseid}\n" +
            "            order by is_top desc , like_count desc , create_time desc\n" +
            "            limit #{offset},#{pageSize}"})

    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "id", property = "favoriteRecords",many = @Many(select = "com.laoli.mapper.CourseCommentFavoriteRecordDao.getFavorites"))
    })
    List<CourseComment> getCommentsByCourseId(@Param("courseid") Integer courseid, @Param("offset") Integer offset, @Param("pageSize") Integer pageSize);

    //更新点赞数量
    @Update({"update course_comment set like_count = like_count + #{x} where id = #{comment_id}"})
    Integer updateLikeCount(@Param("x") Integer x, @Param("comment_id") Integer comment_id);
}